<template>
  <view @touchmove.stop.prevent>
    <view
      class="fab-box"
      :class="{ 'fab-right': !left || (left && right) }"
      :style="{ left: getLeft(), right: getRight(), bottom: bottom + 'rpx' }"
    >
      <view class="fab-btn" :class="{ visible: isOpen, 'fab-hidden': hidden }">
        <view
          class="fab-item-box"
          :class="{ 'fab-item-left': left && !right && item.imgUrl }"
          v-for="(item, index) in btnList"
          :key="index"
          @click.stop="handleClick(index)"
        >
          <view
            :class="[left && !right ? 'text-left' : 'text-right']"
            v-if="item.imgUrl"
            :style="{ fontSize: item.fontSize + 'rpx', color: item.color }"
            >{{ item.text || "" }}</view
          >
          <view
            :class="'fab-item' + index"
            :style="{
              width: width + 'rpx',
              height: height + 'rpx',
              borderRadius: radius,
            }"
          >
            <view
              class="fab-title"
              v-if="!item.imgUrl"
              :style="{ fontSize: item.fontSize + 'rpx', color: item.color }"
              >{{ item.text || "" }}</view
            >
            <image
              :src="item.imgUrl"
              class="fab-img"
              v-else
              :style="{
                width: item.imgWidth + 'rpx',
                height: item.imgHeight + 'rpx',
              }"
            ></image>
          </view>
        </view>
      </view>
      <view
        class="fab-item"
        :class="{ active: isOpen }"
        :style="{
          width: width + 'rpx',
          height: height + 'rpx',
          borderRadius: radius,
          background: bgColor,
          color: color,
        }"
        @click.stop="handleClick(-1)"
      >
        <u-icon name="plus" color="black" size="48"></u-icon>
      </view>
    </view>
    <view
      class="fab-mask"
      :class="{ visible: isOpen }"
      @click="handleClickCancel"
    ></view>
  </view>
</template>

<script>
//拓展出来的按钮不应多于6个，否则违反了作为悬浮按钮的快速、高效的原则
export default {
  name: "tuiFab",
  props: {
    //rpx 为0时值为auto
    left: {
      type: Number,
      default: 0,
    },
    //rpx 当为0时且left不为0，值为auto
    right: {
      type: Number,
      default: 60,
    },
    //rpx bottom值
    bottom: {
      type: Number,
      default: 60,
    },
    //默认按钮 宽度 rpx
    width: {
      type: Number,
      default: 108,
    },
    //默认按钮 高度 rpx
    height: {
      type: Number,
      default: 108,
    },
    //圆角值
    radius: {
      type: String,
      default: "50%",
    },
    //默认按钮背景颜色
    bgColor: {
      type: String,
      default: "#ECF0F3",
    },
    //字体颜色
    color: {
      type: String,
      default: "#ecf0f1",
    },
    fontSize: {
      type: Number,
      default: 34,
    },
    btnList: {
      type: Array,
      default() {
        return []
      },
    },
    //点击遮罩 是否可关闭
    maskClosable: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      isOpen: false,
      hidden: true,
      timer: null,
    }
  },
  methods: {
    getLeft() {
      let val = "auto"
      if (this.left && !this.right) {
        val = this.left + "rpx"
      }
      return val
    },
    getRight() {
      let val = this.right + "rpx"
      if (this.left && !this.right) {
        val = "auto"
      }
      return val
    },
    handleClick: function (index) {
      this.hidden = false
      clearTimeout(this.timer)
      if (index == -1 && this.btnList.length) {
        this.isOpen = !this.isOpen
      } else {
        this.$emit("click", {
          index: index,
        })
        this.isOpen = false
      }
      if (!this.isOpen) {
        this.timer = setTimeout(() => {
          this.hidden = true
        }, 200)
      }
    },
    handleClickCancel: function () {
      if (!this.maskClosable) return
      this.isOpen = false
    },
  },
  beforeDestroy() {
    clearTimeout(this.timer)
    this.timer = null
  },
}
</script>

<style lang="scss" scoped>
.fab-box {
  display: flex;

  position: fixed;
  z-index: 20;

  justify-content: center;
  flex-direction: column;
}

.fab-right {
  align-items: flex-end;
}

.fab-btn {
  visibility: hidden;

  opacity: 0;

  transition: all 0.2s ease-in-out;
  transform: scale(0);
}

.fab-hidden {
  width: 0;
  height: 0;
}

.fab-item-box {
  display: flex;

  padding-bottom: 40rpx;

  align-items: center;
  justify-content: flex-end;
}

.fab-item-left {
  flex-flow: row-reverse;
}

.fab-title {
  overflow: hidden;

  width: 90%;

  text-align: center;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.text-left {
  padding-left: 28rpx;
}

.text-right {
  padding-right: 28rpx;
}

.fab-img {
  display: block;
}

.fab-item {
  display: flex;

  background: #ecf0f3;

  box-shadow: 8px 8px 19px #bbbbbe, -8px -8px 19px #ffffff;

  transition: all 0.2s linear;

  align-items: center;
  justify-content: center;
  &.active {
    background: #ecf0f3;

    box-shadow: inset 7px 7px 14px #c7c7cb, inset -7px -7px 14px #ffffff;

    transform: rotate(135deg);
  }
}

.fab-item1 {
  display: flex;

  background: linear-gradient(
    139deg,
    rgba(0, 188, 212, 1) 0%,
    rgba(139, 195, 74, 1) 100%
  );

  box-shadow: inset 2px 2px 5px rgba(0, 0, 0, 0.25),
    inset -2px -2px 5px rgba(255, 255, 255, 0.25);

  align-items: center;
  justify-content: center;
}

.fab-item0 {
  display: flex;

  background: linear-gradient(
    120deg,
    rgba(255, 152, 0, 1) 0%,
    rgba(233, 30, 99, 1) 100%
  );

  box-shadow: inset 2px 2px 5px rgba(0, 0, 0, 0.25),
    inset -2px -2px 5px rgba(255, 255, 255, 0.25);

  align-items: center;
  justify-content: center;
}

.radius {
  border-radius: 50%;
}

.fab-mask {
  visibility: hidden;

  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 19;

  background: rgba(0, 0, 0, 0.75);

  opacity: 0;

  transition: all 0.2s ease-in-out;
}

.visible {
  visibility: visible;

  opacity: 1;

  transform: scale(1);
}
</style>
